/*
 * @Author: cmd 
 * @wechat: cmd_develop 
 * @qq群: 790373986
 * @Date: 2019-04-23 15:33:22 
 * @Last Modified by: cmd
 * @Last Modified time: 2019-04-23 17:14:25
 * @Description: 
 */
import orc from "../../orc";
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    async http_post(url,params) {
        return new Promise((resolve,reject)=>{
            var xhr = cc.loader.getXMLHttpRequest();
            var time = false;//是否超时
            var timer = setTimeout(function(){
                time = true;
                xhr.abort();//请求中止
                resolve('timeout');
            },5000);
            xhr.onreadystatechange = function () {
                console.log('xhr.readyState='+xhr.readyState+'  xhr.status='+xhr.status);
                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
                    var respone = xhr.responseText;
                    if(time) return;//请求已经超时，忽略中止请求
                    clearTimeout(timer);//取消等待的超时       
                    console.log("url:",url,respone);
                    resolve(respone);
                }               
            };
            let url_temp:string;
           
            url_temp= orc.game_config.HTTP_URL + url;
            xhr.open("POST", url_temp, true);
            // note: In Internet Explorer, the timeout property may be set only after calling the open()
            // method and before calling the send() method.
            //xhr.timeout = 5000;// 5 seconds for timeout
            //xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");             
            xhr.send(params);
        })        
    }
}
